/*
 * Copyright (C) 2010 TTPASI, Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS-IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package ttpasi.jgenere.initial_port.languagedata;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

class LanguageUtils
{
  /**
   * Loads the character frequencies from a String into a Map.
   * <p/>
   * Used by VigCrack upon initialization.
   * 
   * @param     input String with character frequencies data.
   * @return    Map of character frequencies, e.g. A=8.702, B=4.298
   */
  static Map<Character, Double> loadFrequencies(String input)
  {
    Pattern p = Pattern.compile("'[A-Z]':\\d{1,2}\\.\\d{0,4}");
    Matcher m = p.matcher(input);

    Map<Character, Double> frequencies = new HashMap<Character, Double>();
    while (m.find())
    {
      String[] entry = m.group().split(":");
      char k = entry[0].charAt(1);
      double v = Double.parseDouble(entry[1]);
      frequencies.put(k, v);
    }
    return frequencies;
  }
}